import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hixing_app/const/color_res.dart';

class GreyBorderTextField extends StatefulWidget {
  const GreyBorderTextField(
      {Key? key,
      required this.textController,
      this.borderColor,
      this.fillColor = Colors.white,
      this.validator,
      this.disable = false,
      this.suffixIcon,
      this.keyboardType = TextInputType.text,
      this.hintColor = ColorRes.hintTextColor,
      this.radius = 4,
      required this.onChanged,
      this.maxLength,
      this.centerText = false,
      this.hintText = '请输入价格',
      this.obscureText = false,
      this.maxLines = 1,
      this.width,
      this.prefixIcon,
      this.onlyNum = false,
      this.disableBorderColor,
      this.node})
      : super(key: key);

  final TextEditingController textController;
  final String? Function(String?)? validator;
  final double? width;
  final String hintText;
  final bool disable;
  final Color? borderColor;
  final Widget? suffixIcon;
  final Widget? prefixIcon;
  final double radius;
  final TextInputType keyboardType;
  final Color fillColor;
  final Color hintColor;
  final FocusNode? node;
  final int? maxLength;
  final bool obscureText;
  final bool onlyNum;
  final bool centerText;
  final int? maxLines;
  final Color? disableBorderColor;
  final Function(String) onChanged;

  @override
  State<GreyBorderTextField> createState() => _GreyBorderTextFieldState();
}

class _GreyBorderTextFieldState extends State<GreyBorderTextField> {
  void onLengthChange(val) {
    setState(() {});
    widget.onChanged(val);
  }

  @override
  Widget build(BuildContext context) {
    final _themeData = Theme.of(context);
    Widget child = TextFormField(
      maxLength: widget.maxLength,
      controller: widget.textController,
      enabled: !widget.disable,
      onChanged: widget.maxLength != null ? onLengthChange : widget.onChanged,
      validator: widget.validator,
      obscureText: widget.obscureText,
      focusNode: widget.node,
      keyboardType: widget.keyboardType,
      maxLines: widget.maxLines,
      inputFormatters:
          widget.onlyNum ? [FilteringTextInputFormatter.digitsOnly] : [],
      cursorColor: _themeData.primaryColor,
      textAlign: widget.centerText ? TextAlign.center : TextAlign.left,
      style: TextStyle(
          fontSize: 14,
          color: _themeData.primaryColor,
          fontWeight: FontWeight.normal),
      // selectionControls: MaterialTextSelectionControls(),
      decoration: InputDecoration(
        suffixIconConstraints: const BoxConstraints(maxHeight: 40),
        suffixIcon: widget.maxLength != null
            ? Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Container(
                    width: 40,
                    child: Row(
                      children: [
                        Text("${widget.textController.text.length}",
                            style: TextStyle(color: ColorRes.hintTextColor)),
                        Text(
                          '/${widget.maxLength}',
                          style: TextStyle(color: ColorRes.hintTextColor),
                        )
                      ],
                    ),
                  ),
                  widget.suffixIcon ?? SizedBox.shrink(),
                ],
              )
            : widget.suffixIcon,
        isCollapsed: true,
        counterText: '',
        filled: true,
        prefixIcon: widget.prefixIcon,
        enabledBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(widget.radius),
          borderSide:
              BorderSide(color: widget.borderColor ?? _themeData.primaryColor),
        ),
        prefixIconConstraints: const BoxConstraints(
//添加内部图标之后，图标和文字会有间距，实现这个方法，不用写任何参数即可解决
            ),
        focusedBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(widget.radius),
          borderSide:
              BorderSide(color: widget.borderColor ?? _themeData.primaryColor),
        ),
        disabledBorder: widget.disableBorderColor == null
            ? null
            : OutlineInputBorder(
                borderRadius: BorderRadius.circular(widget.radius),
                borderSide:
                    BorderSide(color: _themeData.primaryColor, width: 1),
              ),
        focusedErrorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(widget.radius),
          borderSide: BorderSide(color: _themeData.primaryColor, width: 1),
        ),
        errorBorder: OutlineInputBorder(
          borderRadius: BorderRadius.circular(widget.radius),
          borderSide: BorderSide(color: Colors.red, width: 1),
        ),
        contentPadding: EdgeInsets.only(
            right: 10.0,
            left: widget.centerText ? 10 : 16,
            top: 10,
            bottom: 12),
        fillColor: widget.fillColor,
        hintText: widget.hintText,
        hintStyle: TextStyle(
          color: widget.hintColor,
          fontSize: 14,
        ),
      ),
    );
    if (widget.width != null) {
      child = SizedBox(
        width: widget.width,
        child: child,
      );
    }
    return Theme(
        data: ThemeData.light(), //Themes.red,
        child: child);
  }
}
